package com.lglbc.democase.controller;

import com.alibaba.fastjson.JSONObject;
import com.lglbc.democase.utils.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author： 乐哥聊编程(全平台同号)
 */
@RestController
@RequestMapping("/testIP")
public class TestIPController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${appkey}")
    private String appkey;
    @RequestMapping
    public String testIP(HttpServletRequest request, @RequestParam(required = false) String ip){
        String ipAddr = IPUtils.getIpAddr(request);
        if (StringUtils.hasText(ip)){
            ipAddr = ip;
        }
        JSONObject object = restTemplate.getForObject(String.format("https://apis.map.qq.com/ws/location/v1/ip?ip=%s&key=%s", ipAddr,appkey), JSONObject.class);
        System.out.println(object.toJSONString());
        return "IP属地："+object.getJSONObject("result").getJSONObject("ad_info").getString("province");

    }


}
